<?php
class ACardProduct extends CActiveRecord
{
    public function getAllProviderCode()
    {
        $sql = "SELECT DISTINCT (provider_code) FROM c_card_log WHERE provider_code!='' ORDER BY provider_code";
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll();
        return $rows;
    }
    
    public function GetProduct($search, $currentPage, $numberRecordPerPage)
    {
        $search["fromDate"] .= " 23:59:59";
        $startRecord = ($currentPage - 1) * $numberRecordPerPage;
        
        $condition = "";        
        $condition .= " AND create_date>='" . strtotime($search["toDate"]) . "'";
        $condition .= " AND create_date<='" . strtotime($search["fromDate"]) . "'";        
        $condition .= !empty($search["provider_code"]) ? " AND provider_code='" . mysql_escape_string($search["provider_code"]) . "'" : "";        
        $condition .= !empty($search["price"]) ? " AND price=" . mysql_escape_string($search["price"]) : "";        
        $condition .= !empty($search["type"]) ? " AND type=" . mysql_escape_string($search["type"]) : "";        
        $condition .= " AND status=1";        
        
        if(!empty($search["keyword"])){
            if(empty($search["type"]) || $search["type"]==1){
                
                $sql = "SELECT id FROM c_game WHERE title='" . mysql_escape_string($search["keyword"]) . "'";
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryRow();        
                $condition .= !empty($rows) ? " AND type='" . mysql_escape_string($search["type"]) . "' AND dataId='" . mysql_escape_string($rows["id"]) . "'" : " AND 1=2";
                
            } else if(empty($search["type"]) || $search["type"]==2){
                
                $sql = "SELECT id FROM c_video WHERE title='" . mysql_escape_string($search["keyword"]) . "'";
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryRow();        
                $condition .= !empty($rows) ? " AND type='" . mysql_escape_string($search["type"]) . "' AND dataId='" . mysql_escape_string($rows["id"]) . "'" : " AND 1=2";
                
            } else if(empty($search["type"]) || $search["type"]==3){
                
                $sql = "SELECT id FROM c_album WHERE title='" . mysql_escape_string($search["keyword"]) . "'";
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryRow();        
                $condition .= !empty($rows) ? " AND type='" . mysql_escape_string($search["type"]) . "' AND dataId='" . mysql_escape_string($rows["id"]) . "'" : " AND 1=2";
                
            }                                                
        }
          
        /* Tiêu chí sắp xếp mặc định */      
        $sql = "SELECT dataId, type, sum(price) as total_price FROM c_card_log WHERE 1 " . $condition . " GROUP BY dataId, type ORDER BY total_price DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;        
        
        if($search["order"]=="1") {
            /* Sắp xếp theo doanh thu từ thấp đến cao */
            $sql = "SELECT dataId, type, sum(price) as total_price FROM c_card_log WHERE 1 " . $condition . " GROUP BY dataId, type ORDER BY total_price ASC LIMIT " . $startRecord . ", " . $numberRecordPerPage;        
        } else if($search["order"]=="2") {
            /* Sắp xếp theo doanh thu từ cao đến thấp */
            $sql = "SELECT dataId, type, sum(price) as total_price FROM c_card_log WHERE 1 " . $condition . " GROUP BY dataId, type ORDER BY total_price DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;        
        } else if($search["order"]=="3") {
            /* Sắp xếp theo sản lượng từ thấp đến cao */
            $sql = "SELECT dataId, type, count(id) as total_card FROM c_card_log WHERE 1 " . $condition . " GROUP BY dataId, type ORDER BY total_card ASC LIMIT " . $startRecord . ", " . $numberRecordPerPage;        
        } else if($search["order"]=="4") {
            /* Sắp xếp theo sản lượng từ cao đến thấp */
            $sql = "SELECT dataId, type, count(id) as total_card FROM c_card_log WHERE 1 " . $condition . " GROUP BY dataId, type ORDER BY total_card DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;        
        }  
        
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll();    
        return $rows;
    }
    
    public function GetTotalProduct($search)
    {
        $search["fromDate"] .= " 23:59:59";
        
        $condition = "";        
        $condition .= " AND create_date>='" . strtotime($search["toDate"]) . "'";
        $condition .= " AND create_date<='" . strtotime($search["fromDate"]) . "'";        
        $condition .= !empty($search["provider_code"]) ? " AND provider_code='" . mysql_escape_string($search["provider_code"]) . "'" : "";        
        $condition .= !empty($search["price"]) ? " AND price=" . mysql_escape_string($search["price"]) : "";        
        $condition .= !empty($search["type"]) ? " AND type=" . mysql_escape_string($search["type"]) : "";        
        $condition .= " AND status=1";        
        
        if(!empty($search["keyword"])){
            if(empty($search["type"]) || $search["type"]==1){
                
                $sql = "SELECT id FROM c_game WHERE title='" . mysql_escape_string($search["keyword"]) . "'";
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryRow();        
                $condition .= !empty($rows) ? " AND type='" . mysql_escape_string($search["type"]) . "' AND dataId='" . mysql_escape_string($rows["id"]) . "'" : " AND 1=2";
                
            } else if(empty($search["type"]) || $search["type"]==2){
                
                $sql = "SELECT id FROM c_video WHERE title='" . mysql_escape_string($search["keyword"]) . "'";
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryRow();        
                $condition .= !empty($rows) ? " AND type='" . mysql_escape_string($search["type"]) . "' AND dataId='" . mysql_escape_string($rows["id"]) . "'" : " AND 1=2";
                
            } else if(empty($search["type"]) || $search["type"]==3){
                
                $sql = "SELECT id FROM c_album WHERE title='" . mysql_escape_string($search["keyword"]) . "'";
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryRow();        
                $condition .= !empty($rows) ? " AND type='" . mysql_escape_string($search["type"]) . "' AND dataId='" . mysql_escape_string($rows["id"]) . "'" : " AND 1=2";
                
            }                                                
        }
                
        $sql = "SELECT count(total) FROM (SELECT count(id) as total FROM c_card_log WHERE 1 " . $condition . " GROUP BY dataId, type) temp";          
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryRow();            
        return $rows["count(total)"];
    }
    
    public function getReport($search, $rate_card, $order)
    {        
        $search["fromDate"] .= " 23:59:59";
        
        $condition = "";        
        $condition .= " AND create_date>='" . strtotime($search["toDate"]) . "'";
        $condition .= " AND create_date<='" . strtotime($search["fromDate"]) . "'";        
        $condition .= !empty($search["provider_code"]) ? " AND provider_code='" . mysql_escape_string($search["provider_code"]) . "'" : "";        
        $condition .= !empty($search["price"]) ? " AND price=" . mysql_escape_string($search["price"]) : "";        
        
        $sms = array();
        
        if(!empty($search["list_game_id"])){
            $condition_game = " AND type=1 AND dataId IN (" . mysql_escape_string($search["list_game_id"]) . ")";
            
            $sql = "SELECT count(id), sum(price), price, dataId, percent_wapmaster, percent_partner, month, year FROM c_card_log WHERE status=1 " . $condition_game . $condition . " GROUP BY dataId, price, percent_wapmaster, percent_partner, month, year";        
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();
            
            /* lấy tên game */
            $sql = "SELECT id, title FROM c_game WHERE id IN (" . $search["list_game_id"] . ")";
            $command = Yii::app()->db->createCommand($sql);
            $rowsGame = $command->queryAll();
            $replaceGame = array();
            
            foreach($rowsGame as $row){                
                $replaceGame[$row["id"]]["title"] = $row["title"];
            }
            
            $i = 0;
            while(isset($rows[$i])){                
                $rows[$i]["title"] = isset($replaceGame[$rows[$i]["dataId"]]["title"]) ? $replaceGame[$rows[$i]["dataId"]]["title"] : "";
                $i++;
            }
            
            foreach($rows as $row){
                $id = $row["dataId"];                        
                $type = 1;
                
                $index = $id . "_" . $type;
                                               
                $percent_wapmaster = $row["percent_wapmaster"];
                $percent_partner = $row["percent_partner"];
                            
                if(!isset($card[$index]["total_card"])) $card[$index]["total_card"] = 0;
            
                if(!isset($card[$index]["10000"])) $card[$index]["10000"] = 0;
                if(!isset($card[$index]["20000"])) $card[$index]["20000"] = 0;
                if(!isset($card[$index]["30000"])) $card[$index]["30000"] = 0;
                if(!isset($card[$index]["50000"])) $card[$index]["50000"] = 0;
                
                if(!isset($card[$index]["100000"])) $card[$index]["100000"] = 0;
                if(!isset($card[$index]["200000"])) $card[$index]["200000"] = 0;
                if(!isset($card[$index]["300000"])) $card[$index]["300000"] = 0;
                if(!isset($card[$index]["500000"])) $card[$index]["500000"] = 0;
                
                if(!isset($card[$index]["telco"])) $card[$index]["telco"] = 0;            
                if(!isset($card[$index]["wapmaster"])) $card[$index]["wapmaster"] = 0;
                if(!isset($card[$index]["mtop"])) $card[$index]["mtop"] = 0;
                if(!isset($card[$index]["partner"])) $card[$index]["partner"] = 0;
                
                $card[$index]["title"] = $row["title"];
                $card[$index]["type"] = $type;
                $card[$index]["total_card"] += $row["count(id)"];            
                $card[$index]["10000"] += $row["price"]==10000 ? $row["count(id)"] : 0;
                $card[$index]["20000"] += $row["price"]==20000 ? $row["count(id)"] : 0;
                $card[$index]["30000"] += $row["price"]==30000 ? $row["count(id)"] : 0;
                $card[$index]["50000"] += $row["price"]==50000 ? $row["count(id)"] : 0;
                
                $card[$index]["100000"] += $row["price"]==100000 ? $row["count(id)"] : 0;
                $card[$index]["200000"] += $row["price"]==200000 ? $row["count(id)"] : 0;
                $card[$index]["300000"] += $row["price"]==300000 ? $row["count(id)"] : 0;
                $card[$index]["500000"] += $row["price"]==500000 ? $row["count(id)"] : 0;
                
                $total_money = $row["sum(price)"];
                
                $index_rate = $row["month"] . "_" . $row["year"];
                $rateCard = $rate_card[$index_rate] / 100;
                
                $card[$index]["telco"] += $total_money * $rateCard;              
                $card[$index]["wapmaster"] += $total_money * $rateCard * ($percent_wapmaster / 100);
                $card[$index]["partner"] += $total_money * $rateCard * ($percent_partner / 100);
                $card[$index]["mtop"] += $total_money * $rateCard * ((100 - $percent_partner - $percent_wapmaster) / 100);
            }
        }
          
        $response = array();
        foreach($order as $index=>$i){
            if(isset($card[$index])){
                $response[$index] = $card[$index];
            }   
        }
        
        return $response;
    }
    
    public function getReportByProduct($search, $rate_card, $currentPage, $numberRecordPerPage)
    {        
        $startRecord = ($currentPage - 1) * $numberRecordPerPage;
        
        $search["fromDate"] .= " 23:59:59";
        $condition = "";
        $condition .= " AND dataId=" . mysql_escape_string($search["dataId"]);
        $condition .= " AND type=" . mysql_escape_string($search["type"]);
        $condition .= " AND create_date>='" . strtotime($search["toDate"]) . "'";
        $condition .= " AND create_date<='" . strtotime($search["fromDate"]) . "'";        
        $condition .= !empty($search["provider_code"]) ? " AND provider_code='" . mysql_escape_string($search["provider_code"]) . "'" : "";        
        $condition .= !empty($search["price"]) ? " AND price='" . mysql_escape_string($search["price"]) . "'" : "";                
        $condition .= " AND status=1";                
        
        
        $sql = "SELECT * FROM c_card_log WHERE 1 " . $condition . " ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll();
        
        $i = 0;
        while(isset($rows[$i])){
            $type = $rows[$i]["type"];
            
            $index_rate = $rows[$i]["month"] . "_" . $rows[$i]["year"];
            $rateCard = $rate_card[$index_rate] / 100;
            
            $rows[$i]["telco"] = $rows[$i]["price"] * $rateCard;              
            if($type=="1"){
                $rows[$i]["wapmaster"] = $rows[$i]["telco"] * ($rows[$i]["percent_wapmaster"] / 100);
                $rows[$i]["partner"] = $rows[$i]["telco"] * ($rows[$i]["percent_partner"] / 100);
                $rows[$i]["mtop"] = $rows[$i]["telco"] * ((100 - $rows[$i]["percent_wapmaster"] - $rows[$i]["percent_partner"]) / 100);
            } 
            
            $i++;
        }
        
        return $rows;
    }
    
    public function getTotalReportByProduct($search, $rate_card)
    {        
        $search["fromDate"] .= " 23:59:59";
        $condition = "";
        $condition .= " AND dataId=" . mysql_escape_string($search["dataId"]);
        $condition .= " AND type=" . mysql_escape_string($search["type"]);
        $condition .= " AND create_date>='" . strtotime($search["toDate"]) . "'";
        $condition .= " AND create_date<='" . strtotime($search["fromDate"]) . "'";        
        $condition .= !empty($search["provider_code"]) ? " AND provider_code='" . mysql_escape_string($search["provider_code"]) . "'" : "";        
        $condition .= !empty($search["price"]) ? " AND price='" . mysql_escape_string($search["price"]) . "'" : "";                
        $condition .= " AND status=1";
        
        $sql = "SELECT count(id) FROM c_card_log WHERE 1 " . $condition;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryRow();        
        return $rows["count(id)"];
    }
}  
?>
